package cn.yunbamboos.db.metadata;

import cn.yunbamboos.db.GenerateSqlFactory;
import cn.yunbamboos.db.sql.GenerateSql;
import cn.yunbamboos.exception.AppException;
import cn.yunbamboos.mybatis.annotation.FieldType;

import java.util.ArrayList;
import java.util.List;

public class TableMetadata {
    // 表名称
    private final String name;
    // 注释
    private final String comment;
    // 列
    private final List<ColumnMetadata> columns = new ArrayList<>();

    private final GenerateSql sql = GenerateSqlFactory.generate(this);

    public TableMetadata(String name, String comment) {
        this.name = name;
        this.comment = comment;
    }

    public void addColumn(String name, FieldType type, int length, String comment, int sort) {
        this.columns.add(new ColumnMetadata(name, type, length, comment, sort, false));
    }

    public void addColumn(String name, FieldType type, int length, String comment, int sort, boolean primaryKey) {
        this.columns.add(new ColumnMetadata(name, type, length, comment, sort, primaryKey));
    }

    public String toCreateTableSql(){
        if(sql == null){
            throw AppException.c("generate sql is null");
        }
        return sql.toCreateTableSql();
    }


    public String getName() {
        return name;
    }

    public String getComment() {
        return comment;
    }

    public List<ColumnMetadata> getColumns() {
        return columns;
    }

}
